package com.nbstar.film.spider.excel;

import com.nbstar.film.spider.domain.FilmSpiderDomain;
import com.nbstar.film.spider.pojo.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;

@Data
@Slf4j
public class CommonExcelWriter extends XSSExcelWriter {
    private String spider = FilmSpiderDomain.SPIDER_FROM_MAOYAN;

    public CommonExcelWriter(String sheetName, String excelPath) {
        super(sheetName, excelPath);
    }

    @Override
    protected void initExcelHeader(Row header) {
        header.createCell(0).setCellValue("片名");
        header.createCell(1).setCellValue("地区");
        header.createCell(2).setCellValue("年份");
        header.createCell(3).setCellValue("评分");
        header.createCell(4).setCellValue("评论人数");
        header.createCell(5).setCellValue("票房");
        header.createCell(6).setCellValue("类型");
        header.createCell(7).setCellValue("导演");
        header.createCell(8).setCellValue("演职人员");
        header.createCell(9).setCellValue("剧情简介");
        header.createCell(10).setCellValue("集数");
        header.createCell(11).setCellValue("英文名称");
        header.createCell(12).setCellValue("别名");
        header.createCell(13).setCellValue("时长");


        if (spider.equals(FilmSpiderDomain.SPIDER_FROM_DOUBAN)) {
            header.createCell(14).setCellValue("IMDB");
        }
    }

    @Override
    protected void writeRow(Row row, FilmExcelInfo filmDomain) {
        row.createCell(0).setCellValue(filmDomain.getFilmName());
        row.createCell(1).setCellValue(filmDomain.getArriveAddress());
        row.createCell(2).setCellValue(filmDomain.getArriveDate());
        if (spider.equals(FilmSpiderDomain.SPIDER_FROM_MAOYAN)) {
            row.createCell(3).setCellValue(filmDomain.getMaoyanScore());
            row.createCell(4).setCellValue(filmDomain.getMaoyanVotes());
        }

        if (spider.equals(FilmSpiderDomain.SPIDER_FROM_DOUBAN)) {
            row.createCell(3).setCellValue(filmDomain.getDoubanScore());
            row.createCell(4).setCellValue(filmDomain.getDoubanVotes());
        }

        if (spider.equals(FilmSpiderDomain.SPIDER_FROM_IMDB)) {
            row.createCell(3).setCellValue(filmDomain.getImdbScore());
            row.createCell(4).setCellValue(filmDomain.getImdbVotes());
        }

        row.createCell(5).setCellValue(filmDomain.getBoxOffice());
        row.createCell(6).setCellValue(filmDomain.getCat());
        row.createCell(7).setCellValue(filmDomain.getDirector());
        row.createCell(8).setCellValue(filmDomain.getActor());
        row.createCell(9).setCellValue(filmDomain.getDesc());
        row.createCell(10).setCellValue(filmDomain.getPartNum());
        row.createCell(11).setCellValue(filmDomain.getEname());
        row.createCell(12).setCellValue(filmDomain.getAliasName());
        row.createCell(13).setCellValue(filmDomain.getFileDuration());
        if (spider.equals(FilmSpiderDomain.SPIDER_FROM_DOUBAN)) {
            row.createCell(14).setCellValue(filmDomain.getImdbURL());
        }
    }

    @Override
    public ExcelVedioInfo trans2Excel(FilmSpiderDomain domain) {
        FilmExcelInfo filmExcelInfo = new FilmExcelInfo();
        FilmInfo filmInfo = domain.get(spider);


        filmExcelInfo.setFilmName(domain.getRaw().getFilmName());
        filmExcelInfo.setArriveAddress(filmInfo.getArriveAddress());
        filmExcelInfo.setArriveDate(filmInfo.getArriveDate());
        if (spider.equals(FilmSpiderDomain.SPIDER_FROM_MAOYAN)) {
            filmExcelInfo.setMaoyanScore(filmInfo.getAverage());
            filmExcelInfo.setMaoyanVotes(filmInfo.getVotes());
            if (filmInfo instanceof MaoYanFilmInfo) {
                filmExcelInfo.setBoxOffice(((MaoYanFilmInfo) filmInfo).getBoxOffice());
            }

        }

        if (spider.equals(FilmSpiderDomain.SPIDER_FROM_DOUBAN)) {
            filmExcelInfo.setDoubanScore(filmInfo.getAverage());
            filmExcelInfo.setDoubanVotes(filmInfo.getVotes());

        }

        if (spider.equals(FilmSpiderDomain.SPIDER_FROM_IMDB)) {
            filmExcelInfo.setImdbScore(filmInfo.getAverage());
            filmExcelInfo.setImdbVotes(filmInfo.getVotes());

        }

        filmExcelInfo.setCat(filmInfo.getCategory());
        filmExcelInfo.setDirector(filmInfo.getDirector());
        filmExcelInfo.setActor(filmInfo.getActor());
        filmExcelInfo.setDesc(filmInfo.getDesc());
        filmExcelInfo.setPartNum(filmInfo.getPartNum());
        filmExcelInfo.setEname(filmInfo.getEname());
        filmExcelInfo.setAliasName(filmInfo.getAliasName());
        filmExcelInfo.setFileDuration(filmInfo.getFileDuration());

        if (spider.equals(FilmSpiderDomain.SPIDER_FROM_DOUBAN)) {
            if (filmInfo instanceof DouBanFilmInfo) {
                DouBanFilmInfo douBanFilmInfo = (DouBanFilmInfo) filmInfo;
                filmExcelInfo.setImdbURL(douBanFilmInfo.getImdb());
            }
        }

        return filmExcelInfo;
    }
}
